﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using Newtonsoft.Json;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;

namespace NetDiskPRJ.NetDisk
{
    public partial class DirSelectDlgForm : Base.DlgForm
    {
        public decimal DestDirID = 0;//当前所在的文件夹编号

        public DirSelectDlgForm()
        {
            InitializeComponent();
        }


        #region 左边树菜单

        /// <summary>
        /// 树形列表图片集
        /// </summary>
        private ImageCollection TreeImage = new ImageCollection();

        /// <summary>
        /// 加载界面上的主菜单
        /// <creator>zhongyl</creator>
        /// <date>2011-12-16</date>
        /// </summary>
        private void LoadMainMenu()
        {
            //左边树形菜单
            tlstLeft.BeginUpdate();
            tlstLeft.Columns.Add();
            tlstLeft.Columns[0].Caption = "文件列表";
            tlstLeft.Columns[0].VisibleIndex = 0;
            tlstLeft.Columns.Add();

            TreeImage = base.GetFormImageCollection();
            tlstLeft.StateImageList = TreeImage;
            tlstLeft.OptionsBehavior.ShowToolTips = false;
            //tlstLeft.Painter.IndicatorImages.editorIsInactiveIndicatorImageIndex = -1; 
            tlstLeft.EndUpdate();

            BindLeftTree();
        }

        private void BindLeftTree()
        {
            tlstLeft.BeginUpdate();

            tlstLeft.Nodes.Clear();

            string strJson = Base.GlobalParams.NetDiskServiceSVC.GetDirByLogID(Base.GlobalParams.GLOBAL_LOGID);
            List<Model.NetDisk> dirList = JsonConvert.DeserializeObject<List<Model.NetDisk>>(strJson);

            if (dirList.Count > 0)
            {
                List<Model.NetDisk> dirFirstList = (from m in dirList
                                                    where m.FParentID == null
                                                    select m).ToList();
                foreach (Model.NetDisk dirModel in dirFirstList)
                {

                    //添加节点
                    TreeListNode rootNode = tlstLeft.AppendNode(
                                new object[] { "我的云盘" },
                                null);
                    rootNode.Tag = dirModel.FID;
                    //设置选中节点
                    if (decimal.Parse(rootNode.Tag.ToString()) == DestDirID)
                    {
                        tlstLeft.FocusedNode = rootNode;
                    }
                    //子菜单
                    List<Model.NetDisk> tmpDirList = (from m in dirList
                                                      where m.FParentID == dirModel.FID
                                                      select m).ToList();
                    if (tmpDirList.Count > 0)
                    {
                        GetChildDirList(tmpDirList, rootNode, dirList);
                    }
                    rootNode.StateImageIndex = GetImageIndex("folder_close", TreeImage);
                    rootNode.Expanded = true;
                }
            }

            tlstLeft.EndUpdate();
        }

        /// <summary>
        /// 获取子文件夹
        /// </summary>
        /// <param name="dirChildList"></param>
        /// <param name="parentNode"></param>
        /// <param name="dirList"></param>
        public void GetChildDirList(List<Model.NetDisk> dirChildList, TreeListNode parentNode, List<Model.NetDisk> dirList)
        {
            foreach (Model.NetDisk dirModel in dirChildList)
            {
                //添加节点
                TreeListNode node = tlstLeft.AppendNode(
                            new object[] { dirModel.FName },
                            parentNode);
                node.Tag = dirModel.FID;
                //设置选中节点
                if (decimal.Parse(node.Tag.ToString()) == DestDirID)
                {
                    tlstLeft.FocusedNode = node;
                }

                //子菜单
                List<Model.NetDisk> tmpDirList = (from m in dirList
                                                  where m.FParentID == dirModel.FID
                                                  select m).ToList();

                if (tmpDirList.Count > 0)
                {
                    GetChildDirList(tmpDirList, node, dirList);
                }
                node.StateImageIndex = GetImageIndex("folder_close", TreeImage);
                node.Expanded = true;
            }
        }

        private void tlstLeft_AfterExpand(object sender, NodeEventArgs e)
        {
            e.Node.StateImageIndex = GetImageIndex("folder_open", TreeImage);
        }

        private void tlstLeft_AfterCollapse(object sender, NodeEventArgs e)
        {
            e.Node.StateImageIndex = GetImageIndex("folder_close", TreeImage);
        }

        #endregion

        private void DirSelectDlgForm_Load(object sender, EventArgs e)
        {
            LoadMainMenu();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DestDirID =decimal.Parse(tlstLeft.FocusedNode.Tag.ToString());
            this.DialogResult = DialogResult.OK;
        }
    }
}
